package net.ib.mtalk.account;

import java.util.ArrayList;

import net.ib.mtalk.util.MTalkLogWriter;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.Context;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.provider.ContactsContract;


/**
 * Contacts ContentProvider 에 대해서 batch Operation 수행하는 class
 * @author namkhoh
 *
 */
public class BatchOperation {

	private final ContentResolver mResolver;
	
	ArrayList<ContentProviderOperation> mOperations;
	
	public BatchOperation(Context context, ContentResolver resolver) {
		mResolver = resolver;
		mOperations = new ArrayList<ContentProviderOperation>();
	}
	
	public int size() {
		return mOperations.size();
	}
	
	public void add(ContentProviderOperation cpo) {
		mOperations.add(cpo);
	}
	
	public void execute() {
		if (mOperations.size() == 0) {
			return;
		}
		
		try {
			mResolver.applyBatch(ContactsContract.AUTHORITY, mOperations);
		} catch (OperationApplicationException e) {
			MTalkLogWriter.e(e + " : " + getClass().getName());
		} catch (RemoteException e) {
			MTalkLogWriter.e(e + " : " + getClass().getName());
		}
		mOperations.clear();
	}
}
